import {
  Controller,
  Post,
  Provide,
  Put,
  Get,
  Body,
  Inject,
  Param,
  Del,
  Query,
} from '@midwayjs/core';
import { RoleDTO } from '../dto/role';
import { RoleService } from '../service/role';
import { RuleType, Valid } from '@midwayjs/validate';
import { R } from '../../../common/base.error.util';
import { FindOptionsWhere } from 'typeorm';
import { BaseSysRole } from '../entity/role';

@Provide()
@Controller('/role', { description: '角色管理' })
export class RoleController {
  
  @Inject()
  roleService: RoleService;

  @Post('/', { description: '创建' })
  async create(@Body() data: RoleDTO) {
    return await this.roleService.createRole(data);
  }

  @Put('/', { description: '编辑' })
  async edit(@Body() data: RoleDTO) {
    return await this.roleService.editRole(data);
  }

  @Del('/:id', { description: '删除' })
  async remove(
    @Valid(RuleType.number().required().error(R.error('id不能为空')))
    @Param('id')
    id: number
  ) {
    await this.roleService.deleteRole(id);
  }

  @Get('/:id', { description: '根据id查询' })
  async getById(@Param('id') id: string) {
    return await this.roleService.getById(id);
  }
  
  @Get('/page', { description: '分页查询' })
  async page(
    @Query('page') page: number,
    @Query('size') size: number,
    @Query('nickName') nickName: string,
    @Query('phone') phone: string
  ) {
    const query: FindOptionsWhere<BaseSysRole> = {};
    return await this.roleService.page(page, size, query);
  }
}
